home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / qndxr.2 ƒ / doc_buf.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-11-03  |  3.9 KB  |  180 lines  |  [TEXT/KAHL]

  1. /* file doc_buf.c ...  by ^z 870830-0919-...
  2.  * functions to load in text from the document file and then
  3.  * save out keys and pointers to the *.k and *.p files ...
  4.  * modified 871007-... to unify the doc-buffer loading with the
  5.  * character-filtering and the pointer array building....
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <unix.h>
  11. #include <storage.h>
  12. #include <strings.h>
  13. #include <ctype.h>
  14. #include <proto.h>
  15. #include "qndxr.2.h"
  16.  
  17.  
  18. /* function to create a buffer for me to use...
  19.  */
  20.  
  21. char *make_buf (bufsiz)
  22.   long bufsiz;
  23.   {
  24.     char *buf, *malloc(), *mlalloc();
  25.     void exit();
  26.     
  27.     DEBUG ("--allocating a buffer, size = %ld\n", bufsiz);
  28.     
  29. #ifdef LIGHTSPEED
  30.     buf = mlalloc (bufsiz);
  31. #else
  32.     buf = malloc ((unsigned int)bufsiz);
  33. #endif
  34.  
  35.     if (buf == NULL)
  36.       {
  37.         printf ("\nFatal error in attempt to allocate a buffer!\n");
  38.         printf ("(bufsiz=%ld)\n", bufsiz);
  39.         exit(1);
  40.       }
  41.     
  42.     return (buf);
  43.   }
  44.  
  45.  
  46. /* function to load the document buffer ... bring in doc_bufsiz
  47.  * characters, and then enough more to finish out the final word,
  48.  * followed by a terminal delimiter .... as the characters are read
  49.  * in, filter them appropriately (depending on user choices) and
  50.  * build the pointer array in memory to the first character of each
  51.  * word ... return the total number of words that were
  52.  * read in to the buffer (zero if we're finished with the file)
  53.  *
  54.  * ... note that one must be sure to pull in and throw away
  55.  * any excess characters beyond KEY_LENGTH in the final word, so that
  56.  * the remaining fragment doesn't show up as the first "word" in the
  57.  * next chunk of the file....
  58.  *
  59.  * Routine modified 871007-... in order to unify the buffer-loading and
  60.  * character-filtering and pointer-array-building operations, and to go
  61.  * back to using getc() from <stdio> rather than Macintosh-specific
  62.  * operations for loading the buffer....
  63.  */
  64.  
  65. long load_doc_buffer (doc, doc_bufsiz, ptr)
  66.   char *doc, **ptr;
  67.   long doc_bufsiz;
  68.   {
  69.     int c, i, in_a_word = FALSE;
  70.     char **ptr0, *end_doc_buf;
  71.     extern FILE *doc_file;
  72.  
  73.     DEBUG ("--Loading document buffer...\n", NULL);
  74.      
  75.      ptr0 = ptr;
  76.      end_doc_buf = doc + doc_bufsiz;
  77.      
  78.      while (doc < end_doc_buf)
  79.        {
  80.          c = filtered_getc ();
  81.          DEBUG ("--filtered character = \"%c\"\n", c);
  82.          if (c == EOF)
  83.            {
  84.              *doc++ = '\0';
  85.              in_a_word = FALSE;
  86.              break;
  87.            }
  88.          if (! c)
  89.              in_a_word = FALSE;
  90.          else if (! in_a_word)
  91.            {
  92.              *ptr++ = doc;
  93.              in_a_word = TRUE;
  94.              DEBUG ("--adding new ptr = %ld\n", doc);
  95.            }
  96.          *doc++ = c;
  97.        }
  98.      
  99.      if (doc == end_doc_buf && in_a_word)
  100.       {
  101.         DEBUG ("--finishing off a final buffer word...\n", NULL);
  102.           for (i = 0; i < KEY_LENGTH; ++i)
  103.            {
  104.              c = filtered_getc ();
  105.              if (c == EOF)
  106.                {
  107.                  *doc++ = '\0';
  108.                  break;
  109.                }
  110.              if (! c)
  111.                {
  112.                  *doc++ = '\0';
  113.                  break;
  114.                }
  115.              *doc++ = c;
  116.            }
  117.          if (i == KEY_LENGTH)
  118.              while (filtered_getc ())
  119.                   ;
  120.        }
  121.      
  122.      return (ptr - ptr0);
  123.   }
  124.  
  125.  
  126. /* function to get the next character from the document file and filter it
  127.  * as the user desires ... return:
  128.  *  EOF  if end of file encountered;
  129.  *  '/0' if the character is a delimiter;
  130.  *  otherwise, the character itself (filtered into upper-case,
  131.  *        if it was lower-case)
  132.  */
  133.  
  134. int filtered_getc ()
  135.   {
  136.     static int prevc, c = '\0';
  137.     int nextc;
  138.     extern int keep_all_punct, keep_embedded_punct, keep_special_chars;
  139.     extern FILE *doc_file;
  140.  
  141.     prevc = c;
  142.     c = getc (doc_file);
  143.     
  144.     if (c == EOF)
  145.         return (EOF);
  146.     
  147.     if (islower (c))
  148.         return (c = toupper (c));
  149.     
  150.     if (isupper (c) || isdigit (c))
  151.         return (c);
  152.     
  153.     if (isspace (c))
  154.         return (c = '\0');
  155.     
  156.     if (keep_special_chars && ! isascii (c))
  157.         return (c);
  158.     
  159.     if (keep_all_punct && ispunct (c))
  160.         return (c);
  161.     
  162.     if (keep_embedded_punct && ispunct (c))
  163.       {
  164.         if (prevc == '\0')
  165.             return (c = '\0');
  166.         nextc = getc (doc_file);
  167.         ungetc (nextc, doc_file);
  168.         if (nextc == EOF)
  169.             return (c = '\0');
  170.         if (isalnum (nextc) || (keep_special_chars && ! isascii (nextc)))
  171.             return (c);
  172.         else
  173.             return (c = '\0');
  174.       }
  175.     
  176.     return (c = '\0');
  177.   }
  178.  
  179.  
  180.